package com.team.appinfo.controller;

import com.github.pagehelper.PageInfo;
import com.team.appinfo.entity.*;
import com.team.appinfo.service.AppinfoService;
import com.team.appinfo.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;

@Controller
public class AppInfoController {
    @Autowired
    private AppinfoService appinfoService;

    @RequestMapping("/getAppinfoAll")  //条件查询所有用户下的app信息
    public String getAppinfoAll(AppInfoTiaoJian appInfoTiaoJian, Model model, HttpSession session){
       DevUser du =(DevUser)session.getAttribute("du"); //设置用户的id   获取session的用户编号
       appInfoTiaoJian.setDevid(du.getId());

        PageInfo<AppInfoConstant> pageinfo = this.appinfoService.getAppinfoAll(appInfoTiaoJian);
            model.addAttribute("du",du);
            model.addAttribute("tiaojian",appInfoTiaoJian); //条件
            model.addAttribute("pageInfo",pageinfo ); //数据
            return "developer/appinfolist";

    }

    @RequestMapping("/addAppInfo")
    public String addAppInfo(  //一个上传文件域对应一个cmmonsMultipartFile类或者MultipartFile的对象
            AppInfo appInfo,  //实体类对象
            @RequestParam(value = "a_logoPicPath") CommonsMultipartFile file,
            HttpServletRequest request,
            Model model,
            HttpSession session){   //session用于保存用户的devId
        try {
            //1.上传图片
         String relativePath= SystemConstant.getApp_Add_lujing();  //获取字典里自己写的相对路劲
         String path=request.getSession().getServletContext().getRealPath(relativePath);  //绝对路劲
         String fileName= FileUploadUtil.uploadFile(path,file);  //上传
      if (fileName!=null){
          //2.将信息写入到数据库进行存储
          //设置上传文件
         appInfo.setLogolocpath(path+"/"+fileName);  //绝对路劲
         appInfo.setLogopicpath(relativePath+"/"+fileName);  //相对路径
          //设置当前用记的编号
          DevUser du =(DevUser)session.getAttribute("du");
          appInfo.setDevid(du.getId());  //设置用户的id
          this.appinfoService.addAppInfo(appInfo);//调用业务保存添加
          model.addAttribute("du",du);
          model.addAttribute("errey","添加成功");
          return "redirect:getAppinfoAll";
      }
        } catch (Exception e) {
            model.addAttribute("errey","添加失败");
            e.printStackTrace();
        }
        return "developer/appinfoadd";
    }

    @RequestMapping("/getOneAppInfo")
    public String getOneAppInfo(Long id,HttpSession session,Model model){
        AppInfoConstant oneAppInfo = this.appinfoService.getOneAppInfo(id);
        DevUser du =(DevUser)session.getAttribute("du");
        oneAppInfo.setDevId(du.getId());
        model.addAttribute("du",du);
        model.addAttribute("oneAppInfo",oneAppInfo);
        return "developer/appinfomodify";
    }


    @RequestMapping("/updateAppInfo")
    public String updateAppInfo(
            //一个上传文件域对应一个cmmonsMultipartFile类或者MultipartFile的对象
            AppInfoConstant appInfoConstant,  //实体类对象
            @RequestParam(value = "a_logoPicPath") CommonsMultipartFile file,
            HttpServletRequest request,
            Model model,
            String logoPicPath,//jsp里的隐藏域字段名-->相对路劲
            String logoLocPath,//jsp里的隐藏域字段名-->绝对路劲
            HttpSession session){
        try {
            if (!file.isEmpty()){  //判断用户有没有选择图片,如果选择图片即上传，否则不上传

                String relativePath= SystemConstant.getApp_Add_lujing();  //获取字典里自己写的相对路劲
                String path=request.getSession().getServletContext().getRealPath(relativePath);  //绝对路劲
                String fileName= FileUploadUtil.uploadFile(path,file);  //上传
                 //设置新上传的图片路径↓
                appInfoConstant.setLogoPicPath(relativePath+"/"+fileName);
                //删除原来的图片   1.通过编号去查询数据库得图片的路径  2.表单中获取原图路径
                new File(request.getSession().getServletContext().getRealPath(logoPicPath)).delete();
//                new File(request.getSession().getServletContext().getRealPath(logoLocPath)).delete();
            }

            //更新数据库的信息
            appinfoService.updateAppInfo(appInfoConstant);

        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:getOneAppInfo?id="+appInfoConstant.getId();
        }
        return "redirect:getAppinfoAll";
    }

    @RequestMapping("/deleteAppInfoByAppId")
    public String deleteAppInfoByAppId(
            Long id,HttpServletRequest request,
            String logoLocPath){

        try {
            this.appinfoService.deleteAppInfoByAppId(id);
            String delPath=request.getSession().getServletContext().getRealPath(logoLocPath);
            File delfile=new File(delPath);
            System.out.println("文件名为:"+delfile.toString());
            if (delfile.exists()) { //判断文件是否纯在
                delfile.delete();  //删除旧图
            }
            return "redirect:getAppinfoAll";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:getAppinfoAll";
    }

    @RequestMapping("/shangxiaApp") //上架下架app
    @ResponseBody
    public String shangxiaApp(Long state,Long id){
        int a= this.appinfoService.xiugaiAppInfoZhuangtai(state,id);
        return "{\"result\":"+a+"}";
    }
}
